<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml" xmlns:iftg="http://www.w3.org/1999/xhtml">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
<div class="wrapper wrapper-content ">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form class="form-horizontal m-t" id="signupForm">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">消息类型：</label>
                            <div class="col-sm-8">
                                <iftg:select id="name" name="name" dicType="sendNews"
                                             data-placeholder="选择消息类型" class="form-control chosen-select"
                                             tabindex="2" style="width: 100%" defaultValue="1">
                                </iftg:select>
                                <!--<input id="name" class="form-control" type="text">-->
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">用户电话号码：</label>
                            <div class="col-sm-8">
                                <input id="users" class="form-control" type="text" >
                                <label style="color: red;display: none" id="phone"></label>
                            </div>

                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">短信模板：</label>
                            <div class="col-sm-8">
                                <select id="smsDemo" class="form-control">

                                </select>
                            </div>
                        </div>
                        <div id="signBox"></div>
                        <div class="form-group">
                            <div class="col-sm-8 col-sm-offset-3">
                                <button type="button" id="submit" class="btn btn-primary">发送</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:include="include::footer"></div>
<script>
    function isPoneAvailable($poneInput) {

        // var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
        // if (!myreg.test($poneInput.val())) {
        //     return false;
        // } else {
        //     return true;
        // }
    }

    $(function () {
        $("#users").change(function () {
            var  phone= $(this).val();
            var valid_rule = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/;// 手机号码校验规则
            if ( ! valid_rule.test(phone)) {
                $("#phone").html("手机号码格式有误");
                $("#phone").css("display","inline")
            }else{
                $("#phone").css("display","none")
            }
        });
        $.ajax({
            url: "/jzweb/send/messageS",
            type: "post",
            success: function (res) {
                var list = res.data;
                var sendList = '<option value="">-请选择-</option>';
                for (var i = 0; i < list.length; i++) {
                    sendList += '<option value="' + list[i].id + '" data-sign="' + list[i].sign + '">' + list[i].name + '</option>'
                }
                $("#smsDemo").html(sendList);
                console.log("res", res)
            }
        });
        $("#smsDemo").change(function () {
            var that = $(this).val();
            $.ajax({
                url: "/jzweb/send/messageSon",
                data: {id: that},
                type: "post",
                success: function (res) {
                    var sendList = '';
                    var a = res.data.messageContent.split(',');
                    for (var j = 0; j < a.length; j++) {
                        sendList += '<div class="form-group sendB">' +
                            '<label class="col-sm-3 control-label" id="sendKey' + j + '">' + a[j] + '</label>' +
                            '<div class="col-sm-8">' +
                            '<input id="send' + j + '" class="form-control" />' +
                            '</div></div>'
                    }
                    $("#signBox").html(sendList)
                }
            });
        });
        $("#submit").click(function () {
                console.log($("#smsDemo").val())
            if( $("#smsDemo").val() == '1585920079737704450' ){
                var users = $("#users").val();
                var smsDemo = $("#smsDemo").val();
                var sms = '';
                $(".sendB").each(function (index, element) {
                    var a = $(this).find("#sendKey" + index).html();
                    var b = $(this).find("#send" + index).val();
                    var c = a + ':' + b;
                    if (index == 0) {
                        sms = c;
                    } else {
                        sms = sms + ';' + c;
                    }
                });
                $.ajax({
                    url: "/jzweb/send/send1s",
                    data: {
                        // name: name,
                        users: users,
                        id: smsDemo,
                        messageContent: sms
                    },
                    type: "post",
                    success: function (res) {
                        if(res.code==200){
                            layer.msg("操作成功！");
                        }else {
                            layer.msg(res.msg);
                        }
                    }
                });
            }else if( $("#smsDemo").val() == '1587339952766373890'){
                var users = $("#users").val();
                var smsDemo = $("#smsDemo").val();
                var sms = '';
                $(".sendB").each(function (index, element) {
                    var a = $(this).find("#sendKey" + index).html();
                    var b = $(this).find("#send" + index).val();
                    var c = a + ':' + b;
                    if (index == 0) {
                        sms = c;
                    } else {
                        sms = sms + ';' + c;
                    }
                });
                $.ajax({
                    url: "/jzweb/send/sendStudy",
                    data: {
                        // name: name,
                        users: users,
                        id: smsDemo,
                        messageContent: sms
                    },
                    type: "post",
                    success: function (res) {
                        if(res.code==200){
                            layer.msg(res.msg);
                        }else {
                            layer.msg(res.msg);
                        }
                    }
                });


            }else {

                var users = $("#users").val();
                var smsDemo = $("#smsDemo").val();
                var sms = '';
                $(".sendB").each(function (index, element) {
                    var a = $(this).find("#sendKey" + index).html();
                    var b = $(this).find("#send" + index).val();
                    var c = a + ':' + b;
                    if (index == 0) {
                        sms = c;
                    } else {
                        sms = sms + ';' + c;
                    }
                });
                $.ajax({
                    url: "/jzweb/send/send",
                    data: {
                        // name: name,
                        users: users,
                        id: smsDemo,
                        messageContent: sms
                    },
                    type: "post",
                    success: function (res) {
                    }
                });
            }
        }

        )
    })

</script>
</body>
</html>
